/**
 * Created by cchen on 2015/5/19.
 */
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length == 1 ? o[k] :
                ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}
if (!String.prototype.format) {
    String.prototype.format = function () {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function (match, number) {
            return typeof args[number] != 'undefined'
                ? args[number]
                : match;
        });
    };
}


'use strict';
angular.module('backofficeApp.ProductManager.controller', []).
    controller('clickSourceCtrl', ['$scope', '$http', function ($scope, $http) {

        $scope.date = new Date()

        $scope.clickSourceData = []

        $scope.getClickSource = function (start, end) {
            var startDate = start == null || start == '' ? 'all' : start
            var endDate = end == null || end == '' ? 'all' : end

            $('#loadingText').html('Loading...')

            if (startDate === 'all')
                $('#startDateText').html('开始')
            else
                $('#startDateText').html(startDate)

            if (endDate === 'all')
                $('#endDateText').html(new Date().format('yyyy-MM-dd hh:mm:ss'))
            else
                $('#endDateText').html(endDate)
            getClickSourceDataAPI(startDate, endDate)
        }

        function getClickSourceDataAPI(startDate, endDate) {
            $http.get('http://' + backOfficeHost + '/rest/v1.0/BI/reports/getClickSource/' + startDate + '/' + endDate, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.clickSourceData = data.data
                $('#loadingText').html('')
            }).error(function () {
            });
        }

        // Init page
        $scope.getClickSource('all', 'all')

        // Sort columns
        $scope.orderByField = 'total';
        $scope.reverseSort = true;

    }])